/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.components.tabs.tabpanel.components;

import it.blueocean.acanto.model.MenuItem;
import it.blueocean.acanto.model.NavigationItem;
import it.blueocean.acanto.web.platform.components.tabs.tabpanel.ButtonPanelContainer;

import java.lang.reflect.Constructor;
import java.util.SortedSet;

import org.apache.log4j.Logger;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.security.extensions.markup.html.tabs.ISecureTab;

public class DynamicTabs implements ISecureTab, Comparable<Object>
{
	private static final long serialVersionUID = -6812519317968202791L;

	private static final Logger logger = Logger.getLogger(DynamicTabs.class);

	private String tabTitle;

	private String tabId;

	private int tabPosition;

	private SortedSet<MenuItem> tabChildren;

	private ButtonPanelContainer buttonsContainer;

	private Panel classForSecurityInstance;

	public DynamicTabs(NavigationItem navigationItem)
	{
		setTabId(navigationItem.getId());
		setTabTitle(navigationItem.getTitle());
		setTabPosition(navigationItem.getPosition());
		setTabChildren(navigationItem.getChildren());

		init(navigationItem.getId(), navigationItem.getChildren());
	}

	private void init(String tabId, SortedSet<MenuItem> children)
	{
		setButtonsContainer(new ButtonPanelContainer(tabId, children));

		try
		{
			Class<?> c = Class.forName(getTabId());
			Constructor<?> constructor = c.getConstructor(new Class[] { String.class });

			setContentPanelForSecurity((Panel) constructor.newInstance(tabId));
		}
		catch (Exception e)
		{
			logger.error("Unable to instantiate a custom panel class for DynamicTab", e);
		}

	}

	private void setContentPanelForSecurity(Panel thisInstance)
	{
		this.classForSecurityInstance = thisInstance;
	}

	private Panel getClassForSecurityInstance()
	{
		return this.classForSecurityInstance;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class<Panel> getPanel()
	{
		return (Class<Panel>) getClassForSecurityInstance().getClass();
	}

	@Override
	public Panel getPanel(String panelId)
	{
		init(panelId, getTabChildren());

		return getButtonsContainer();
	}

	@Override
	public IModel<String> getTitle()
	{
		return new Model<String>(getTabTitle());
	}

	@Override
	public int compareTo(Object obj)
	{
		int result = this.getTabPosition() - ((DynamicTabs) obj).getTabPosition();

		if(result < 0)
			return -1;
		else if(result == 0)
			return 0;
		else
			return 1;
	}

	public int getTabPosition()
	{
		return tabPosition;
	}

	public void setTabPosition(int tabPosition)
	{
		this.tabPosition = tabPosition;
	}

	public String getTabTitle()
	{
		return tabTitle;
	}

	public void setTabTitle(String tabTitle)
	{
		this.tabTitle = tabTitle;
	}

	public String getTabId()
	{
		return tabId;
	}

	public void setTabId(String menuId)
	{
		this.tabId = menuId;
	}

	public SortedSet<MenuItem> getTabChildren()
	{
		return tabChildren;
	}

	public void setTabChildren(SortedSet<MenuItem> tabChildren)
	{
		this.tabChildren = tabChildren;
	}

	public ButtonPanelContainer getButtonsContainer()
	{
		return buttonsContainer;
	}

	public void setButtonsContainer(ButtonPanelContainer buttonsContainer)
	{
		this.buttonsContainer = buttonsContainer;
	}

	@Override
	public boolean isVisible()
	{
		// TODO Auto-generated method stub
		return false;
	}

}
